package com.novax.ex.activity.provider.controller.inner;

import com.novax.ex.activity.infrastructure.entity.RankConfig;
import com.novax.ex.activity.open.api.inner.RankConfigInnerApi;
import com.novax.ex.activity.open.model.request.RankConfigRequest;
import com.novax.ex.activity.open.model.response.RankApplyUserResponse;
import com.novax.ex.activity.open.model.response.RankConfigResponse;
import com.novax.ex.activity.provider.controller.BaseActivityController;
import com.novax.ex.activity.provider.service.RankApplyService;
import com.novax.ex.activity.provider.service.RankConfigService;
import com.novax.ex.common.constant.InternationalCode;
import com.novax.ex.common.enums.RankStatus;
import com.novax.ex.common.results.ReturnResult;
import com.novax.ex.common.util.CopyUtils;
import com.novax.ex.common.util.PageQuery;
import com.novax.ex.common.util.PageUtil;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RestController;

import jakarta.annotation.Resource;
import java.util.List;
import java.util.Objects;

@RestController
public class RankConfigInnerController extends BaseActivityController implements RankConfigInnerApi {

    @Resource
    private RankConfigService rankConfigService;
    @Resource
    private RankApplyService rankApplyService;

    @Override
    public ReturnResult<PageUtil<RankConfigResponse>> page(PageQuery pageQuery) {
        PageUtil<RankConfigResponse> page = new PageUtil<>(pageQuery);
        rankConfigService.findPage(page, RankConfigResponse.class);
        return ReturnResult.success(page);
    }

    @Override
    public ReturnResult<?> add(String language, RankConfigRequest rankConfigRequest, BindingResult bindingResult) {
        if(bindingResult.hasErrors()){
            return ReturnResult.fail(getInternational(language, InternationalCode.LACK_VALUE));
        }
        // 新建活动默认关闭
        rankConfigRequest.setStatus(RankStatus.close.getStatus());
        boolean flag = rankConfigService.addRankConfig(rankConfigRequest);
        return flag ? ReturnResult.success(getInternational(language, InternationalCode.OPERATION_SUCCESS)) :
            ReturnResult.fail(getInternational(language, InternationalCode.OPERATION_ERROR));
    }

    @Override
    public ReturnResult<?> modify(String language, Long rankId, RankConfigRequest rankConfigRequest) {
        RankConfig currentRankConfig = rankConfigService.find(rankId);
        if(Objects.isNull(currentRankConfig)){
            return ReturnResult.fail(getInternational(language, InternationalCode.NOT_FOUND));
        }

        // 当前修改的活动上架中
        if(currentRankConfig.getStatus().equals(RankStatus.open.getStatus())){
            // 关闭上架的活动
            if(rankConfigRequest.getStatus().equals(RankStatus.close.getStatus())){
                rankConfigRequest.setStartTime(null);
                rankConfigRequest.setEndTime(null);
                rankConfigRequest.setName(null);
            } else {
                return ReturnResult.fail(getInternational(language, InternationalCode.ON_EVENT_SHELF_MODIFY));
            }

        }

        // 如果要上架活动, 已有上架的活动则不可上架
        if(rankConfigRequest.getStatus().equals(RankStatus.open.getStatus())){
            RankConfig openingRankConfig = rankConfigService.findOpenWithOutTime();
            if(Objects.nonNull(openingRankConfig)){
                return ReturnResult.fail(getInternational(language, InternationalCode.ACTIVE_SHELF_ABL));
            }
        }

        RankConfig rankConfig = CopyUtils.copyObject(rankConfigRequest, RankConfig.class);
        rankConfig.setId(rankId);
        boolean flag = rankConfigService.modifySelective(rankConfig);
        return flag ? ReturnResult.success(getInternational(language, InternationalCode.OPERATION_SUCCESS)) :
            ReturnResult.fail(getInternational(language, InternationalCode.OPERATION_ERROR));
    }

    @Override
    public ReturnResult<List<RankApplyUserResponse>> queryApplyUserIdList(Long rankId) {
        return ReturnResult.success(CopyUtils.copyList(rankApplyService.queryApplyUserIdList(rankId), RankApplyUserResponse.class));
    }

}
